package com.ruoyi.manage.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import com.alipay.api.domain.OrderDetail;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.manage.domain.OrderDTO;
import com.ruoyi.manage.mapper.OrderDetailMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.OrderMapper;
import com.ruoyi.manage.domain.Order;
import com.ruoyi.manage.service.IOrderService;

/**
 * 订单管理Service业务层处理
 * 
 * @author rjgc-17
 * @date 2024-12-25
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 查询订单管理
     * 
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public Order selectOrderById(Long id)
    {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单管理列表
     * 
     * @param order 订单管理
     * @return 订单管理
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增订单管理
     * 
     * @param order 订单管理
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateTime(DateUtils.getNowDate());
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单管理
     * 
     * @param order 订单管理
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        order.setUpdateTime(DateUtils.getNowDate());
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单管理
     * 
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids)
    {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单管理信息
     * 
     * @param id 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id)
    {
        return orderMapper.deleteOrderById(id);
    }

    /**
     * 创建订单
     * @param orderDTO
     * @return
     */
    public Order createOrder(OrderDTO orderDTO) {
        Order order = new Order();
        BeanUtils.copyProperties(orderDTO, order);
        order.setOrderTime(DateUtils.getNowDate());
        order.setId(UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE);
        order.setUserId(SecurityUtils.getUserId());
        order.setUpdateTime(DateUtils.getNowDate());
        order.setUpdateBy(SecurityUtils.getUsername());
        order.setCreateBy(SecurityUtils.getUsername());
        order.setCreateTime(DateUtils.getNowDate());
        orderMapper.insertOrder(order);

        com.ruoyi.manage.domain.OrderDetail orderDetail = new com.ruoyi.manage.domain.OrderDetail();
        BeanUtils.copyProperties(orderDTO, orderDetail);
        orderDetail.setQuantity(Long.valueOf(orderDTO.getQuantity()));
        orderDetail.setOrderId(order.getId());
        // 先将 String 类型的总金额转换为 BigDecimal 类型
        BigDecimal totalAmount = new BigDecimal(String.valueOf(orderDTO.getTotalAmount()));
        // 获取数量（假设是 Integer 类型，根据实际定义调整）
        Integer quantity = orderDTO.getQuantity();
        // 计算单价，通过 BigDecimal 的 divide 方法进行除法运算，第二个参数表示保留的小数位数，第三个参数表示舍入模式
        BigDecimal price = totalAmount.divide(new BigDecimal(quantity), 2, BigDecimal.ROUND_HALF_UP);
        // 设置单价到 orderDetail 对象中
        orderDetail.setPrice(price);
        orderDetail.setCreateTime(DateUtils.getNowDate());
        orderDetail.setUpdateTime(DateUtils.getNowDate());
        orderDetail.setCreateBy(SecurityUtils.getUsername());
        orderDetail.setUpdateBy(SecurityUtils.getUsername());
        orderDetailMapper.insertOrderDetail(orderDetail);
        return order;
    }
}
